package interview.hw_od;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Scanner;

/**
 * 比赛的冠亚季军 https://codefun2000.com/p/P1330
 */
public class Contest {

    /**
     * 思路：抽象出运动员类Player并实现Comparable接口。用一个list记录每轮的胜者，一个list记录每轮的败者。最后当胜者length为2时退出循环
     * 最终胜者队中剩余两个玩家，决出冠亚。败者队中剩余1个或两个玩家，决出季军
     */
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        //注意：给出了要读取的数量就用nextInt读，没给出就用nextLine读
        String[] ss = sc.nextLine().split(" ");
        List<Player> playerList = new ArrayList<>();
        for (int id = 0;id < ss.length;id++) {
            playerList.add(new Player(id, Long.parseLong(ss[id])));
        }
        int length = playerList.size();
        List<Player> loseList = new ArrayList<>();
        while (length >= 3) {
            int i = 0, cur = 0;
            loseList = new ArrayList<>();//记录当前轮的败者
            //相邻玩家两两对决，胜者压入当前列表
            for (;i < length - 1;i++) {
                Player p1 = playerList.get(i);
                Player p2 = playerList.get(++i);
                Player winner = p1.compareTo(p2) > 0 ? p1 : p2;
                playerList.set(cur++, winner);
                loseList.add(p1.compareTo(p2) > 0 ? p2 : p1);
            }
            //若剩余一个轮空
            if (i == length - 1)
                playerList.set(cur++, playerList.get(i));
            length = cur;
        }
        //最终胜者队中剩余两个玩家，败者队中剩余1个或两个玩家
        List<Player> winList = playerList.subList(0, 2);
        //按规则升序排列
        Collections.sort(winList);
        Collections.sort(loseList);
        System.out.println(winList.get(1).id + " " + winList.get(0).id + " " + loseList.get(loseList.size()-1).id);
    }

    static class Player implements Comparable<Player> {
        public int id;
        public long strength;

        public Player(int id, long strength) {
            this.id = id;
            this.strength = strength;
        }

        @Override
        public int compareTo(Player p2) {
            if (this.strength == p2.strength) {
                return Integer.compare(this.id, p2.id);
            }
            return Long.compare(this.strength, p2.strength);
        }
    }
}
